package org.jeecg.modules.carinfo.util;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.util.RestUtil;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;

@Component
public class QueryEnv {

    public boolean getAngMei(String vin,String plateNo,String plateColor,String company) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("userid", "8f094429-ee2a-465c-85c5-8f7cc6206502");
        jsonObject.put("vin", vin);
        HttpHeaders headers=new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);

        HttpResponse response =HttpRequest.post("https://www.51macc.com/api/Mattrio/VinInterface/query_vin_hgz").form(jsonObject.getInnerMap()).execute();
        String s =response.body();
        JSONObject result = JSONObject.parseObject(s);
        if (result.getInteger("recode") == 0) {
            String envType = result.getJSONObject("result").getJSONObject("Vechicle").getJSONObject("VechicleRoot").getString("PFBZ");
            if (envType.contains("国Ⅴ")|| envType.contains("国五")) {
                addEnv(plateNo,plateColor,company,"国五");
                return true;
            }
            if (envType.contains("国Ⅵ") || envType.contains("国六")) {
                addEnv(plateNo,plateColor,company,"国六");
                return true;
            }
        }
        return false;
    }

    public int  getLocal(String plateNo) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("PLATE", plateNo);
        String result = HttpUtil.post("https://enviroment.tw-tj.com:2443/ApiService/GetLocal", jsonObject.toString());
        JSONArray res = JSONObject.parseObject(result).getJSONArray("additional");
        if (res.size() == 0||res.getJSONObject(0).getString("ENVIROMENT").equals("无结果")) {
            return 0;
        } else if (res.getObject(0, JSONObject.class).getString("ENVIROMENT").equals("国五") || res.getObject(0, JSONObject.class).getString("ENVIROMENT").equals("国六")) {
            return 1;
        }
        return -1;
    }

    public void addEnv(String plateNo,String plateColor,String company,String env){
        JSONObject param=new JSONObject();
        param.put("entername",company);
        param.put("plate",plateNo);
        param.put("pcolor",plateColor);
        param.put("enviroment",env);
        param.put("cartype","货车");
        String  res = HttpUtil.post("https://enviroment.tw-tj.com:2443/ApiService/AddLocal", param.toString());
    }

    public int getZhongJiao(String plateNo,String plateColor,String company) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("plate", plateNo);
        jsonObject.put("pcolor", plateColor.equals("黄色")?"2":"1");
        jsonObject.put("entername",company);
        String  result = HttpUtil.post("https://enviroment.tw-tj.com:2443/ApiService/GetZjxl", jsonObject);
        String env=JSONObject.parseObject(result).getString("message");
        if("无结果".contains(env)){
            return 0;
        }
        addEnv(plateNo,plateColor,company,env);
      if(env.contains("国五")||env.contains("国六")){
          return 1;
      }

      return 0;
    }
}
